package org.cn.lennon.mybatis.uitl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionUtil {

	/**
	 * 给对象设置值
	 * @param object
	 * @param field
	 * @param value
	 */
	public static void setValue(Object object, Field field, Object value) {
		try {
			Method method = object.getClass().getMethod(getSetMethodNameByFiledName(field.getName()), field.getType());
			try {
				method.invoke(object, new Object[]{value});
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 通过字段名称拼接处set方法
	 * 
	 * @param filedName
	 * @return
	 */
	private static String getSetMethodNameByFiledName(String filedName) {
        String first = filedName.substring(0, 1);
        String tail = filedName.substring(1);
        return "set" + first.toUpperCase() + tail;
    }
}
